﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using LNPS_DynamicScriptService;

/// <summary>
/// Summary description for DynamicsClass
/// </summary>
public class DynamicsClass
{
	public DynamicsClass()
	{
		//
		// TODO: Add constructor logic here
		//
	}

    public string ExecuteScript(string data)
    {
        // Aif Service Client
        LNPS_DynamicScriptClassClient client = new LNPS_DynamicScriptClassClient();

        // Create an instance of the CallContext class.
        CallContext context = new CallContext();

        // Set the company
        try
        {
            context.Company = (string)HttpContext.Current.Session["companyCode"];
        }
        catch (Exception ex)
        {
            return (ex.Message + ": " + ex.StackTrace);
        }

        // attempt remote execution
        try
        {
            data = "str dynamicScript() { \n" + data + " \n}";
            string result = client.runScript(context, data);
            try
            {
                if (result.Substring(0, 5) == "Error")
                {
                    HttpContext.Current.Session["status"] = "Error";
                    HttpContext.Current.Session["message"] = result;
                }
            }
            catch { }
            return result;
        }
        catch (Exception ex)
        {
            return (ex.Message + ": " + ex.StackTrace);
        }
    }
}
